//
//  BirthPickerView.m
//  WTM
//
//  Created by lxj on 2017/12/5.
//  Copyright © 2017年 付凡. All rights reserved.
//

#import "BirthPickerView.h"

@interface BirthPickerView()

@property(nonatomic ,strong) UIView *headerView;
@property(nonatomic ,strong) UIView *backView;
@property(nonatomic ,strong) UIButton *sureBtn;
@property(nonatomic ,strong) UIButton *cancleBtn;

@end

@implementation BirthPickerView
-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.backView];
        [self addSubview:self.picker];
        [self addSubview:self.headerView];

        [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.bottom.mas_equalTo(self);
        }];
        [self.picker mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.mas_equalTo(self);
            make.height.mas_equalTo(220);
        }];
        [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(self);
            make.height.mas_equalTo(44);
            make.bottom.mas_equalTo(self.picker.mas_top);
        }];
        [self.cancleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.mas_equalTo(self.headerView);
            make.width.mas_equalTo(44);
        }];
        [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.top.bottom.mas_equalTo(self.headerView);
            make.width.mas_equalTo(44);
        }];
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
}
//时间选择器
-(void)dateChange:(NSDate *)date{
    DLog(@"%@",date);
}
-(void)sureClick{
    if (self.pickerCallBack) {
        self.pickerCallBack(self.picker.date, YES);
    }
}
-(void)cancleClick{
    DLog(@"取消");
    if (self.pickerCallBack) {
        self.pickerCallBack(nil, NO);
    }
}
-(void)mbClick{
    if (self.pickerCallBack) {
        self.pickerCallBack(nil, NO);
    }
}
- (UIView *)backView{
    if (!_backView) {
        _backView = [[UIView alloc] init];
        _backView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(mbClick)];
        [_backView addGestureRecognizer:tap];
        _backView.backgroundColor = [UIColor blackColor];
        _backView.alpha = 0.3;
    }
    return _backView;
}

- (UIDatePicker *)picker{
    if (!_picker) {
        _picker = [[UIDatePicker alloc] init];
        _picker.backgroundColor = WhiteColor;
        _picker.datePickerMode = UIDatePickerModeDate;
        [_picker setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_Hans_CN"]];

        NSDateFormatter *formate = [[NSDateFormatter alloc] init];
        [formate setDateFormat:@"yyyy-MM-dd"];
        NSDate *miniDate = [formate dateFromString:@"1900-01-01"];
        _picker.minimumDate = miniDate;
        _picker.maximumDate = [NSDate date];
        [_picker addTarget:self action:@selector(dateChange:)forControlEvents:UIControlEventValueChanged];
    }
    return _picker;
}
- (UIView *)headerView{
    if (!_headerView) {
        // 设置键盘头部
        _headerView = [[UIView alloc] init];
        _headerView.backgroundColor = KBackColor;
        [_headerView addSubview:self.sureBtn];
        [_headerView addSubview:self.cancleBtn];
    }
    return _headerView;
}
- (UIButton *)sureBtn{
    if (!_sureBtn) {
        _sureBtn = [[UIButton alloc] initWithFrame:CGRectZero text:@"完成" textcolor:KRedColor backgroundcolor:nil fontSize:Font_16.pointSize haveCorner:NO];
        [_sureBtn addTarget:self action:@selector(sureClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureBtn;
}
- (UIButton *)cancleBtn{
    if (!_cancleBtn) {
        _cancleBtn = [[UIButton alloc] initWithFrame:CGRectZero text:@"取消" textcolor:KBlackColor backgroundcolor:nil fontSize:Font_16.pointSize haveCorner:NO];
        [_cancleBtn addTarget:self action:@selector(cancleClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancleBtn;
}

@end
